package com.ruoyi.car.business.web.controller;

import com.ruoyi.car.business.domain.BusCarPackageAudit;
import com.ruoyi.car.business.query.CarPackageAuditQuery;
import com.ruoyi.car.business.service.IBusCarPackageAuditService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TablePageInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 套餐审核Controller
 * 
 * @author ruoyi
 * @date 2022-02-04
 */
@Controller
@RequestMapping("business/carPackageAudit")
public class BusCarPackageAuditController extends BaseController
{
    private String prefix = "business/carPackageAudit";

    @Autowired
    private IBusCarPackageAuditService busCarPackageAuditService;

    @RequiresPermissions("business:carPackageAudit:view")
    @GetMapping("/listPage")
    public String audit()
    {
        return prefix + "/list";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("business:carPackageAudit:view")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<BusCarPackageAudit> query(CarPackageAuditQuery qo){
        qo.setUserId(ShiroUtils.getUserId());
        return busCarPackageAuditService.query(qo);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @RequiresPermissions("business:carPackageAudit:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BusCarPackageAudit busCarPackageAudit)
    {
        List<BusCarPackageAudit> list = busCarPackageAuditService.selectBusCarPackageAuditList(busCarPackageAudit);
        ExcelUtil<BusCarPackageAudit> util = new ExcelUtil<BusCarPackageAudit>(BusCarPackageAudit.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @RequiresPermissions("business:carPackageAudit:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BusCarPackageAudit busCarPackageAudit)
    {
        return toAjax(busCarPackageAuditService.insertBusCarPackageAudit(busCarPackageAudit));
    }

    /**
     * 修改【请填写功能名称】
     */
    @RequiresPermissions("business:carPackageAudit:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        BusCarPackageAudit busCarPackageAudit = busCarPackageAuditService.selectBusCarPackageAuditById(id);
        mmap.put("busCarPackageAudit", busCarPackageAudit);
        return prefix + "/edit";
    }

    /**
     * 修改保存【请填写功能名称】
     */
    @RequiresPermissions("business:carPackageAudit:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BusCarPackageAudit busCarPackageAudit)
    {
        return toAjax(busCarPackageAuditService.updateBusCarPackageAudit(busCarPackageAudit));
    }

    /**
     * 删除【请填写功能名称】
     */
    @RequiresPermissions("business:carPackageAudit:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(busCarPackageAuditService.deleteBusCarPackageAuditByIds(ids));
    }

    // 获取流程图
    @RequestMapping(value = "/processImg")
    @ResponseBody
    public void processImg(Long id, HttpServletResponse response) throws IOException {
        InputStream inputStream=busCarPackageAuditService.getProcessImg(id);
        IOUtils.copy(inputStream,response.getOutputStream());
    }

    /**
     * 套餐审核撤销
     */
    @RequestMapping(value = "/cancelApply")
    @ResponseBody
    public AjaxResult cancelApply(Long id) throws IOException {
        busCarPackageAuditService.cancelApply(id);
        return AjaxResult.success();
    }

    @RequestMapping("/todoPage")
    public String todoPage(){
        return prefix + "/todoPage";
    }

    /**
     * 我的待办列表
     */
    @RequestMapping("/todoQuery")
    @ResponseBody
    public TablePageInfo<BusCarPackageAudit> todoQuery(CarPackageAuditQuery qo){
        // 查询当前登录用户的审核数据
        qo.setAuditorId(ShiroUtils.getUserId());
        return busCarPackageAuditService.query(qo);
    }

    @RequestMapping("/auditPage")
    public String auditPage(Long id, Model model){
        model.addAttribute("id", id);
        return prefix + "/auditPage";
    }

    /**
     * 流程审批
     */
    @RequestMapping("/audit")
    @ResponseBody
    public AjaxResult audit(Long id, int auditStatus, String info){
        busCarPackageAuditService.audit(id, auditStatus, info);
        return AjaxResult.success();
    }

    @RequestMapping("/donePage")
    public String donePage(){
        return prefix + "/donePage";
    }

    /**
     * 我的已办列表
     */
    @RequestMapping("/doneQuery")
    @ResponseBody
    public TablePageInfo<BusCarPackageAudit> doneQuery(CarPackageAuditQuery qo){
        qo.setAuditUserIds("["+ShiroUtils.getUserId().toString()+"]");
        return busCarPackageAuditService.query(qo);
    }
}
